package com.example.hello.client.Form;

import com.example.hello.BootstrapForm;
import com.github.gwtbootstrap.client.ui.constants.FormType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ComponentConnector;
import com.vaadin.client.ConnectorHierarchyChangeEvent;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentContainerConnector;
import com.vaadin.shared.Connector;
import com.vaadin.shared.ui.Connect;

@Connect(BootstrapForm.class)
public class BootstrapFormConnector extends AbstractComponentContainerConnector {

    @Override
    protected Widget createWidget() {
        return GWT.create(VBootstrapForm.class);
    }

    @Override
    public BootstrapFormState getState() {
        return (BootstrapFormState) super.getState();
    }

    @Override
    public VBootstrapForm getWidget() {
        return (VBootstrapForm) super.getWidget();
    }

    @Override
    public void onStateChanged(StateChangeEvent stateChangeEvent) {
        super.onStateChanged(stateChangeEvent);

        BootstrapFormState state = getState();

        VBootstrapForm form = getWidget();

        for (Connector connector : state.getWidgets()) {
            form.add(((ComponentConnector) connector).getWidget());
        }
        form.setTarget(state.getTarget());
        form.setType(FormType.valueOf(state.getType()));
    }

    @Override
    public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent connectorHierarchyChangeEvent) {

    }

    @Override
    public void updateCaption(ComponentConnector connector) {

    }

}